# -*- coding: utf-8 -*-
import scrapy
from homeworkMovieTop100.items import Homeworkmovietop100Item

class MoviePagesSpider(scrapy.Spider):
    name = 'movie_pages'
    allowed_domains = ['mtime.com']
    start_urls = ['http://www.mtime.com/top/movie/top100/']
    
    #该方法负责提取response所包含的信息
    def parse(self, response):
        #处理分页 /following-sibling::b[1]
        new_links = response.xpath('//div[@id="PageNavigator"]/a[@class="on"]/following-sibling::a[1]/@href').extract()
        #print (new_links)
        #print ("linksLength:"+str(len(new_links)))
        if new_links and len(new_links) > 0:
            new_link = new_links[0] 
            yield scrapy.Request(new_link,callback=self.parse)
    
        #response.xpath('//div[@class="top_list"]/ul/li/div[@class="mov_con"]/h2/a/text()').extract()
        #此处遍历
        for topList in response.xpath('//div[@class="top_list"]/ul/li'):
            item = Homeworkmovietop100Item() 
            title = topList.xpath('./div[@class="mov_pic"]/a/@title').extract_first()
            spliterIndex = title.find("/")
            if spliterIndex > -1:
                leftTitle = title 
            item["title"] = leftTitle
            
            item["director"] = topList.xpath('./div[@class="mov_con"]/p/a/text()').extract_first()
            score1 = topList.xpath('./div[@class="mov_point"]/b/span[@class="total"]/text()').extract_first()
            score2 = topList.xpath('./div[@class="mov_point"]/b/span[@class="total2"]/text()').extract_first()
            #print ("title:%s,score1:%s，score2:%s",title,score1,score2)
            item["score"] = 0
            if score1 is not None:
                if score2 is not None:
                    scoreStr = score1 + score2
                    item["score"] = float(scoreStr)
            
            yield item
        pass
